<template>
  <div style="height: 100%; width: 100%">
    <div class="table-component-page">
      <!-- 操作栏 -->
      <div class="header-box">
        <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item prop="noticeTitle" label="内容标题">
            <el-input v-model="searchForm.noticeTitle" placeholder="请输入内容标题" />
          </el-form-item>
          <el-form-item prop="typeId" label="内容类型">
            <el-select v-model="searchForm.typeId" placeholder="请选择内容类型" clearable>
              <el-option v-for="item in contentTypeList" :key="item.id" :label="item.typeName" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item prop="name" label="创建人">
            <el-input v-model="searchForm.name" placeholder="请输入创建人" />
          </el-form-item>
          <el-form-item prop="status" label="状态">
            <el-select v-model="searchForm.status" placeholder="请选择内容状态" clearable>
              <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button v-permission="'contentAudit:search'" type="primary" icon="el-icon-search" @click="submitForm">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="info" @click="formCancel">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 内容列表 -->
      <div class="table-container">
        <div class="table-box">
          <el-table
            stripe
            :data="contentData"
            :height="'100%'"
            border
            :header-cell-style="{textAlign:'center'}"
          >
            <el-table-column prop="noticeTitle" label="内容标题" align="center" />
            <el-table-column prop="typeName" label="内容类型" align="center" />
            <el-table-column prop="name" label="创建人" align="center" />
            <el-table-column prop="createTime" label="创建时间" align="center" />
            <el-table-column prop="remark" label="备注" align="center" />
            <el-table-column prop="status" label="状态" align="center">
              <template slot-scope="scope">
                <el-tag :type="statusTagMap[scope.row.status]" :effect="scope.row.status === 4 ? 'plain' : 'dark'">
                  {{ statusList.find(i => i.value === scope.row.status)['label'] }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="auto">
              <template slot-scope="scope">
                <el-button
                  v-permission="'contentAudit:preview'"
                  type="primary"
                  size="mini"
                  @click="checkContent(scope.row)"
                >预览</el-button>
                <el-dropdown v-if="scope.row.status === 1" style="margin-left: 10px">
                  <el-button
                    v-permission="'contentAudit:audit'"
                    type="warning"
                    size="mini"
                  >审核</el-button>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="auditContent(true, scope.row)">通过</el-dropdown-item>
                    <el-dropdown-item @click.native="auditContent(false, scope.row)">驳回</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
                <template v-if="[0,2,5].includes(scope.row.status)">
                  <el-button
                    v-permission="'contentAudit:edit'"
                    type="success"
                    size="mini"
                    @click="editContent(scope.row)"
                  >编辑</el-button>
                  <el-button
                    v-permission="'contentAudit:delete'"
                    type="danger"
                    size="mini"
                    @click="deleteContent(scope.row)"
                  >删除</el-button>
                </template>
                <el-button
                  v-if="scope.row.status === 3"
                  v-permission="'contentAudit:submit'"
                  type="success"
                  size="mini"
                  @click="publishContent(scope.row)"
                >发布</el-button>
                <el-button
                  v-if="scope.row.status === 4"
                  v-permission="'contentAudit:withdraw'"
                  type="danger"
                  size="mini"
                  @click="revokeContent(scope.row)"
                >撤稿</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="table-pagination">
          <!-- 分页 -->
          <el-pagination
            :current-page="pagination.current"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pagination.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pagination.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { auditContent, ContentPublish, ContentWithdraw, GetContentList, GetContentTypeList } from '@/api/submit'
import { DeleteList } from '@/api/submit';
import { CONTENT_STATUS_LIST } from '@/constant';
import store from '@/store';

export default {
  name: 'ContentAudit',
  components: {},
  props: {},
  data() {
    return {
      statusList: CONTENT_STATUS_LIST,
      // 列表数据
      contentData: [],
      multipleSelection: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0
      },
      // 搜索
      searchForm: {},
      contentTypeList: [],
      statusTagMap: {
        0: 'info',
        1: 'success',
        2: 'primary',
        3: 'warning',
        4: 'info',
        5: 'danger'
      }
    }
  },
  computed: {},
  watch: {},
  mounted() {
    this.getList();
    this.getContentType();
  },
  methods: {
    getContentType() {
      const formData = new FormData()
      // 第一个值是key,第二个是value
      formData.append('current', 1)
      formData.append('size', 999)
      const params = new URLSearchParams(formData)
      GetContentTypeList(params).then(res => {
        if (res.code === 200) {
          this.contentTypeList = res.data.records;
        }
      });
    },
    getContentTypeName(id) {
      for (let i = 0; i < this.contentTypeList.length; i++) {
        if (this.contentTypeList[i].id === id) {
          return this.contentTypeList[i].typeName
        } else {
          continue;
        }
      }
    },
    // 分页
    handleCurrentChange(val) {
      this.pagination.current = val;
      this.getList();
    },
    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.getList();
    },
    submitForm() {
      this.getList();
    },
    formCancel() {
      this.$refs['searchForm'].resetFields();
      this.searchForm = {};
    },
    checkContent(item) {
      const arr = ['现任领导', '主要职能', '机构设置']
      if (arr.indexOf(item.typeName) !== -1) {
        if (item.typeName === '现任领导') {
          const obj = {
            id: item.id,
            type: 'preview'
          }
          this.$router.push({ path: '/depIntroduce', query: obj });
        } else if (item.typeName === '主要职能') {
          const obj = {
            id: item.id,
            type: 'preview'
          }
          this.$router.push({ path: '/mainFunction', query: obj });
        } else {
          const obj = {
            id: item.id,
            type: 'preview'
          }
          this.$router.push({ path: '/mechanismSetup', query: obj });
        }
      } else {
        const obj = {
          id: item.id,
          type: 'preview'
        }
        this.$router.push({ path: '/article', query: obj });
      }
    },
    // 审核
    auditContent(flag, item) {
      if (flag) {
        auditContent(true, { id: item.id }).then(res => {
          console.log(11111, res);
          if (res.code === 200) {
            this.$message.success('内容审核通过');
            this.getList();
          }
        });
      } else {
        this.$prompt('请输入驳回原因', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(({ value }) => {
          auditContent(false, { id: item.id, remark: value }).then(res => {
            console.log(11111, res);
            if (res.code === 200) {
              this.$message.success('内容驳回成功');
              this.getList();
            }
          });
        });
      }
    },
    // 发布
    publishContent(item) {
      this.$confirm('确定发布当前内容?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        ContentPublish({ id: item.id }).then(res => {
          if (res.code === 200) {
            this.$message.success('发布成功');
            this.getList();
          }
        });
      });
    },
    // 撤回
    revokeContent(item) {
      ContentWithdraw({ id: item.id }).then(res => {
        if (res.code === 200) {
          this.$message.success('撤回成功');
          this.getList();
        }
      });
    },
    editContent(item) {
      store.commit('content/setContentInfo', item);
      this.$router.push('/contentRelease');
    },
    // 删除
    deleteContent(item) {
      this.$confirm('确定删除当前内容?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        DeleteList({ ids: item.id }).then(res => {
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList();
          }
        })
      })
    },
    getList() {
      const requestBody = {
        ...this.searchForm,
        current: this.pagination.current,
        size: this.pagination.pageSize
      }
      GetContentList(requestBody).then(res => {
        if (res.code === 200) {
          this.contentData = res.data.records;
          this.pagination.total = res.data.total;
        }
      });
    }
  }
}
</script>

<style scoped lang="scss">
  .table-component-page{
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    .header-box{
      width: 100%;
      background-color: #fff;
      padding: 10px 15px;
      ::v-deep .el-form-item{
        margin-right: 30px;
        .el-form-item__label{
          font-family: "Microsoft YaHei";
          color: #333
        }
      }
    }
    .table-container{
      width: 100%;
      flex: 1;
      margin-top: 10px;
      display: flex;
      flex-direction: column;
      .table-box {
        width: 100%;
        flex-grow: 1;
        overflow: hidden;
        overflow-y: auto;
      }
      .table-pagination{
        height: 60px;
        width: 100%;
        text-align: right;
        background-color: #fff;
        margin-top: 10px;
        display: flex;
        align-items: center;
        justify-content: flex-end;
        ::v-deep .el-pagination{
          span, button, ul>li{
            height: 50px;
            line-height: 50px;
            font-size: 16px;
          }
        }
      }
    }
  }

</style>
